<template>
	<view>
		<fui-input label="单位名称:" ic readonly :value="unitNmae"></fui-input>
		<fui-input label="单位唯一标识符:" readonly :value="unitUUID"></fui-input>
		<fui-input label="行政区域:" readonly :value="unitDistrict"></fui-input>


		<view v-if="newRecordTable">
			<u-divider textColor="#2979ff" lineColor="#2979ff" text="灭火器安全检查记录表"></u-divider>
			<up-collapse :border="false">
				<up-collapse-item :isLink="true" title="型号列表">
					<view :key="index" label="型号:" v-for="(item,index) in JSON.parse(newRecordTable.t1)">
						<up-swipe-action>
							<up-swipe-action-item @click="onDelField(1,index)" v-model:show="show"
								:options="[{text:'删除',style: { backgroundColor: '#f56c6c'}}]">
								<view class="swipe-action up-border-top up-border-bottom">
									<view class="swipe-action__content">
										<text class="swipe-action__content__text">型号：</text>
										<text>
											{{item}}
										</text>
									</view>
								</view>
							</up-swipe-action-item>
						</up-swipe-action>
					</view>
					<br />
					<up-button @click="addControllerModels(1)" style="width: 80%; margin: 0 auto;"
						text="添加型号"></up-button>
				</up-collapse-item>
			</up-collapse>

			<fui-input label="管理部门:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t2"></fui-input>
			<fui-input label="编号:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t3"></fui-input>

			<u-divider textColor="#2979ff" lineColor="#2979ff" text="消防控制室值班记录表"></u-divider>

			<!-- <fui-input label="控制器型号:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t4"></fui-input> -->
			<up-collapse :border="false">
				<up-collapse-item :isLink="true" title="型号列表">
					<view :key="index" label="型号:" v-for="(item,index) in JSON.parse(newRecordTable.t4)">
						<up-swipe-action>
							<up-swipe-action-item v-model:show="show" @click="onDelField(4,index)"
								:options="[{text:'删除',style: { backgroundColor: '#f56c6c'}}]">
								<view class="swipe-action up-border-top up-border-bottom">
									<view class="swipe-action__content">
										<text class="swipe-action__content__text">型号：</text>
										<text>
											{{item}}
										</text>
									</view>
								</view>
							</up-swipe-action-item>
						</up-swipe-action>
					</view>
					<br />
					<up-button @click="addControllerModels(4)" style="width: 80%; margin: 0 auto;"
						text="添加型号"></up-button>
				</up-collapse-item>
			</up-collapse>
			
			<u-divider textColor="#2979ff" lineColor="#2979ff" text="消防栓检查表"></u-divider>
			<fui-input label="设备编号:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t5"></fui-input>
			<fui-input label="位置:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t6"></fui-input>

			<u-divider textColor="#2979ff" lineColor="#2979ff" text="建筑消防设施维护保养和故障维修记录表"></u-divider>
			<fui-input label="设备参数:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t7"></fui-input>
			<fui-input label="额定功率:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t8"></fui-input>
			<fui-input label="责任人:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t9"></fui-input>
			<fui-input label="保养人:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t10"></fui-input>
			<fui-input label="审核人:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t11"></fui-input>

			<u-divider textColor="#2979ff" lineColor="#2979ff" text="动用明火记录表"></u-divider>
			<fui-input label="动火地点:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t12"></fui-input>
			<fui-input label="动火范围:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t13"></fui-input>
			<fui-input label="动火现场监护人:" :readonly="isReadonly" placeholder="请输入"
				v-model="newRecordTable.t14"></fui-input>
			<fui-input label="动火项目负责人:" :readonly="isReadonly" placeholder="请输入"
				v-model="newRecordTable.t15"></fui-input>
			<fui-input label="动火方案:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t16"></fui-input>


			<u-divider textColor="#2979ff" lineColor="#2979ff" text="消防测试记录表"></u-divider>
			<fui-section title="消防报警主机及电源测试情况" isLine></fui-section>
			<fui-input label="工程名称:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t17"></fui-input>
			<br />
			<fui-section title="正压风机" isLine></fui-section>
			<fui-input label="编号:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t18"></fui-input>
			<br />
			<fui-section title="排烟风机" isLine></fui-section>
			<fui-input label="编号:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t19"></fui-input>
			<br />
			<fui-section title="水泵接合器检查情况" isLine></fui-section>
			<fui-input label="检查位置:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t20"></fui-input>
			<br />
			<fui-section title="室内消火栓检查情况" isLine></fui-section>
			<fui-input label="检查位置:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t21"></fui-input>
			<br />
			<fui-section title="消防水池位情况" isLine></fui-section>
			<fui-input label="检查位置:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t22"></fui-input>
			<br />
			<!-- <fui-section title="消防水池位情况" isLine></fui-section>
			<fui-input label="检查位置:" :readonly="isReadonly" placeholder="请输入" v-model="newRecordTable.t23"></fui-input> -->
		</view>

		<view v-if="!newRecordTable">
			<u-divider textColor="#2979ff" lineColor="#2979ff" text="灭火器安全检查记录表"></u-divider>
			<fui-input label="型号:" placeholder="请输入" v-model="recordTable.t1"></fui-input>
			<fui-input label="管理部门:" required placeholder="请输入" v-model="recordTable.t2"></fui-input>
			<fui-input label="编号:" required placeholder="请输入" v-model="recordTable.t3"></fui-input>

			<u-divider textColor="#2979ff" lineColor="#2979ff" text="消防控制室值班记录表"></u-divider>
			<fui-input label="控制器型号:" placeholder="请输入" v-model="recordTable.t4"></fui-input>

			<u-divider textColor="#2979ff" lineColor="#2979ff" text="消防栓检查表"></u-divider>
			<fui-input label="设备编号:" required placeholder="请输入" v-model="recordTable.t5"></fui-input>
			<fui-input label="位置:" required placeholder="请输入" v-model="recordTable.t6"></fui-input>

			<u-divider textColor="#2979ff" lineColor="#2979ff" text="建筑消防设施维护保养和故障维修记录表"></u-divider>
			<fui-input label="设备参数:" required placeholder="请输入" v-model="recordTable.t7"></fui-input>
			<fui-input label="额定功率:" required placeholder="请输入" v-model="recordTable.t8"></fui-input>
			<fui-input label="责任人:" required placeholder="请输入" v-model="recordTable.t9"></fui-input>
			<fui-input label="保养人:" required placeholder="请输入" v-model="recordTable.t10"></fui-input>
			<fui-input label="审核人:" required placeholder="请输入" v-model="recordTable.t11"></fui-input>

			<u-divider textColor="#2979ff" lineColor="#2979ff" text="动用明火记录表"></u-divider>
			<fui-input label="动火地点:" required placeholder="请输入" v-model="recordTable.t12"></fui-input>
			<fui-input label="动火范围:" required placeholder="请输入" v-model="recordTable.t13"></fui-input>
			<fui-input label="动火现场监护人:" required placeholder="请输入" v-model="recordTable.t14"></fui-input>
			<fui-input label="动火项目负责人:" required placeholder="请输入" v-model="recordTable.t15"></fui-input>
			<fui-input label="动火方案:" required placeholder="请输入" v-model="recordTable.t16"></fui-input>

			<u-divider textColor="#2979ff" lineColor="#2979ff" text="消防测试记录表"></u-divider>
			<fui-section title="消防报警主机及电源测试情况" isLine></fui-section>
			<fui-input label="工程名称:" required placeholder="请输入" v-model="recordTable.t17"></fui-input>
			<br />
			<fui-section title="正压风机" isLine></fui-section>
			<fui-input label="编号:" required placeholder="请输入" v-model="recordTable.t18"></fui-input>
			<br />
			<fui-section title="排烟风机" isLine></fui-section>
			<fui-input label="编号:" required placeholder="请输入" v-model="recordTable.t19"></fui-input>
			<br />
			<fui-section title="水泵接合器检查情况" isLine></fui-section>
			<fui-input label="检查位置:" required placeholder="请输入" v-model="recordTable.t20"></fui-input>
			<br />
			<fui-section title="室内消火栓检查情况" isLine></fui-section>
			<fui-input label="检查位置:" required placeholder="请输入" v-model="recordTable.t21"></fui-input>
			<br />
			<fui-section title="消防水池位情况" isLine></fui-section>
			<fui-input label="检查位置:" required placeholder="请输入" v-model="recordTable.t22"></fui-input>
			<br />

			<!-- <fui-section title="消防水池位情况" isLine></fui-section>
			<fui-input label="检查位置:" required placeholder="请输入" v-model="recordTable.t23"></fui-input> -->
		</view>

		<br />
		<br />
		<view style="width: 80%; display: flex; justify-content: center; margin: 0 auto; text-align: center;">
			<view style="width: 100%;">
				<up-button type="primary" v-if="!newRecordTable" @click="Submit" text="提交"></up-button>

				<up-button type="primary" v-if="newRecordTable" @click="SetData" text="修改"></up-button>
			</view>
		</view>
		<br />
		<br />
	</view>
</template>

<script>
	import {
		BASE_IP
	} from '@/common/http/url.js'
	export default {
		data() {
			return {
				unitNmae: "",
				unitUUID: "",
				unitDistrict: "",
				recordTable: {
					id: "",
					unitId: "",
					t1: "",
					t2: "",
					t3: "",
					t4: "",
					t5: "",
					t6: "",
					t7: "",
					t8: "",
					t9: "",
					t10: "",
					t11: "",
					t12: "",
					t13: "",
					t14: "",
					t15: "",
					t16: "",
					t17: "",
					t18: "",
					t19: "",
					t20: "",
					t21: "",
					t22: "",
					t23: "",
					isDelete: 0
				},
				newRecordTable: "",
				isReadonly: true,
				mid: 2,
				list: {
					workName: '',
					address: '',
					type: '',
					name: '',
					mobile: '',
					area: '',
					aa: '',
					checked: '',
					id: '',
					userExpirationTime: "",
					unitDistrict: ""
				}
			}
		},
		methods: {
			onDelField(mid,index){
				console.log(mid)
				console.log(index)
				let url = BASE_IP + "pc/presetFeatureSettin/del/field/" + this.unitUUID + "/" + index + "/" + mid
				let user = uni.getStorageSync("user")
				uni.request({
					header: {
						"Authorization": user.token,
						'content-type': 'application/x-www-form-urlencoded'
					},
					method: "DELETE",
					url: url,
					success: (res) => {
						console.log(res)
						if (res.statusCode == 200) {
							if (res.data == false) {
								uni.showToast({
									icon: "error",
									title: "提交失败"
								})
								return
							}
							uni.showToast({
								icon: "success",
								title: "提交成功"
							})
							this.init()
						}
						if (res.statusCode == 500) {
							uni.showToast({
								icon: "error",
								title: "提交失败"
							})
							return
						}
						if (res.statusCode == 404) {
							uni.showToast({
								icon: "error",
								title: "提交失败"
							})
							return
						}
					},
					fail: () => {
						uni.showToast({
							icon: "error",
							title: "服务器出现问题"
						})
					}
				})
			},
			addControllerModels(mid) {
				let _this = this
				uni.showModal({
					title: "添加型号",
					placeholderText: "请输入型号",
					editable: true,
					success: (res) => {
						if (res.confirm) {
							if (!res.content) {
								uni.showToast({
									title: "型号为空",
									icon: "error"
								})
								return
							}
							if (mid == 4) {
								let url = BASE_IP + "pc/presetFeatureSettin/up/field/" + this.unitUUID + "/" +
									res.content + "/4"
								_this.upDataPresetFeatureSetTin(url)
							}
							if (mid == 1) {
								let url = BASE_IP + "pc/presetFeatureSettin/up/field/" + this.unitUUID + "/" +
									res.content + "/1"
								_this.upDataPresetFeatureSetTin(url)
							}
						}
					}
				})
			},
			upDataPresetFeatureSetTin(url) {
				console.log(url)
				let user = uni.getStorageSync("user")
				uni.request({
					header: {
						"Authorization": user.token,
						'content-type': 'application/x-www-form-urlencoded'
					},
					method: "POST",
					url: url,
					success: (res) => {
						console.log(res)
						if (res.statusCode == 200) {
							if (res.data == false) {
								uni.showToast({
									icon: "error",
									title: "提交失败"
								})
								return
							}
							uni.showToast({
								icon: "success",
								title: "提交成功"
							})
							this.init()
						}
						if (res.statusCode == 500) {
							uni.showToast({
								icon: "error",
								title: "提交失败"
							})
							return
						}
						if (res.statusCode == 404) {
							uni.showToast({
								icon: "error",
								title: "提交失败"
							})
							return
						}
					},
					fail: () => {
						uni.showToast({
							icon: "error",
							title: "服务器出现问题"
						})
					}
				})
			},
			SetData() {
				if (this.newRecordTable != "") {
					this.mid = 1
				}
				if (this.newRecordTable == "") {
					this.mid = 2
				}
				this.recordTable.t1 = ""
				this.recordTable.t2 = this.newRecordTable.t2
				this.recordTable.t3 = this.newRecordTable.t3
				this.recordTable.t4 = ""
				this.recordTable.t5 = this.newRecordTable.t5

				this.recordTable.t6 = this.newRecordTable.t6
				this.recordTable.t7 = this.newRecordTable.t7
				this.recordTable.t8 = this.newRecordTable.t8
				this.recordTable.t9 = this.newRecordTable.t9
				this.recordTable.t10 = this.newRecordTable.t10

				this.recordTable.t11 = this.newRecordTable.t11
				this.recordTable.t12 = this.newRecordTable.t12
				this.recordTable.t13 = this.newRecordTable.t13
				this.recordTable.t14 = this.newRecordTable.t14
				this.recordTable.t15 = this.newRecordTable.t15

				this.recordTable.t16 = this.newRecordTable.t16
				this.recordTable.t17 = this.newRecordTable.t17
				this.recordTable.t18 = this.newRecordTable.t18
				this.recordTable.t19 = this.newRecordTable.t19
				this.recordTable.t20 = this.newRecordTable.t20

				this.recordTable.t21 = this.newRecordTable.t21
				this.recordTable.t22 = this.newRecordTable.t22
				this.recordTable.t23 = this.newRecordTable.t23


				this.newRecordTable = ""
			},
			Submit() {
				this.recordTable.unitId = this.unitUUID
				this.isReadonly = !this.isReadonly
				// let json = JSON.stringify(this.recordTable)

				console.log(this.recordTable)


				// if (this.recordTable.t1 == "") {
				// 	uni.showToast({
				// 		icon: "error",
				// 		title: "表单填写不完整"
				// 	})
				// 	return
				// }


				if (this.recordTable.t2 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}

				if (this.recordTable.t3 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}

				// if (this.recordTable.t4 == "") {
				// 	uni.showToast({
				// 		icon: "error",
				// 		title: "表单填写不完整"
				// 	})
				// 	return
				// }

				if (this.recordTable.t5 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}

				if (this.recordTable.t6 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}

				if (this.recordTable.t7 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}

				if (this.recordTable.t8 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}

				if (this.recordTable.t9 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}

				if (this.recordTable.t10 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}

				if (this.recordTable.t11 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}
				if (this.recordTable.t12 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}
				if (this.recordTable.t13 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}
				if (this.recordTable.t14 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}
				if (this.recordTable.t15 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}

				if (this.recordTable.t16 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}
				if (this.recordTable.t17 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}
				if (this.recordTable.t18 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}
				if (this.recordTable.t19 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}
				if (this.recordTable.t20 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}
				if (this.recordTable.t21 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}
				if (this.recordTable.t22 == "") {
					uni.showToast({
						icon: "error",
						title: "表单填写不完整"
					})
					return
				}
				// if (this.recordTable.t23 == "") {
				// 	uni.showToast({
				// 		icon: "error",
				// 		title: "表单填写不完整"
				// 	})
				// 	return
				// }

				let url = ""
				if (this.mid == 1) {
					url = BASE_IP + "pc/presetFeatureSettin/up"
					console.log("更新")
					// return
				} else if (this.mid == 2) {
					url = BASE_IP + "pc/presetFeatureSettin/save"
				}

				console.log(url)

				let user = uni.getStorageSync("user")
				uni.request({

					header: {
						"Authorization": user.token,
						'content-type': 'application/x-www-form-urlencoded'
					},
					method: "POST",
					url: url,
					data: {
						presetFeatureSetTinStr: JSON.stringify(this.recordTable)
					},
					dataType: "JSON",
					success: (res) => {
						console.log(res)
						if (res.statusCode == 200) {
							console.log(res)
							uni.showToast({
								icon: "success",
								title: "提交成功"
							})
							this.init()
						}
						if (res.statusCode == 500) {
							uni.showToast({
								icon: "error",
								title: "提交失败"
							})
							return
						}
						if (res.statusCode == 404) {
							uni.showToast({
								icon: "error",
								title: "提交失败"
							})
							return
						}
					},
					fail: () => {
						uni.showToast({
							icon: "error",
							title: "服务器出现问题"
						})
					}
				})



			},
			init() {
				let url = BASE_IP + "pc/presetFeatureSettin/" + this.unitUUID
				console.log(url)
				let user = uni.getStorageSync("user")
				uni.request({
					header: {
						"Authorization": user.token
					},
					url: url,
					success: (res) => {
						console.log(res)
						uni.setStorageSync("recordTable", res.data)

						let recordTable = res.data
						// console.log(recordTable)
						if (recordTable != "") {
							this.newRecordTable = recordTable
						} else {
							this.newRecordTable = ""
						}

					},
					fail: (res) => {
						uni.showToast({
							title: "服务器请求失败",
							icon: "error"
						})
						return
					}
				})
			},
			queryUnit() {
				let user = uni.getStorageSync("user")
				// console.log("用户对象：",user)
				let url = BASE_IP + "pc/unit/query/" + user.userName
				uni.request({
					url: url,
					header: {
						"Authorization": user.token
					},
					success: (res) => {
						if (res.statusCode == 200 && res.data != '') {

							this.list.name = res.data.safetyName
							this.list.type = res.data.landPlanningType
							this.list.mobile = res.data.safetyPhone
							this.list.workName = res.data.unitName
							this.list.address = res.data.unitAddress
							this.list.aa = res.data.unitArea
							this.list.id = res.data.unitID
							this.list.checked = res.data.unitType
							this.list.userExpirationTime = user.userExpirationTime
							this.list.unitDistrict = res.data.unitDistrict
							console.log(res.data)
							let unit = this.list
							this.unitNmae = unit.workName
							this.unitUUID = unit.id
							this.unitDistrict = unit.unitDistrict
							this.init()
						}

						// console.log("获取单位对象：",res)
					},
					fail: () => {
						uni.showToast({
							title: "服务器出现问题",
							icon: "error"
						})
					}
				})

			}
		},
		onLoad() {
			this.queryUnit()



			// console.log(unit)
			// setTimeout(() => {


			// }, 800)
			// // this.recordTable = recordTable
			// console.log(recordTable)
		}
	}
</script>

<style>
	.u-demo-block__title {
		padding: 10px 0 2px 15px;
	}

	.swipe-action__content {
		padding: 25rpx 0;
	}

	.swipe-action__content__text {
		font-size: 15px;
		font-weight: bold;
		/* 替换为实际的颜色值 */
		padding-left: 30rpx;
	}
</style>